package democlone;

import java.util.Arrays;

//Clonable 接口和深拷贝

/*class Person implements Cloneable {
    //实现接口Cloneable---浅拷贝
    public String name;

    public Person(String name) {
        this.name = name;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person("张三");
        Person person2 = (Person)person.clone();
        System.out.println(person2.name);

    }
}*/
class Money implements Cloneable {
    public double money = 9.9;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable {
    //实现接口Cloneable---深拷贝
    public String name;
    public Money m;

    public Person(String name) {
        this.name = name;
        m = new Money();
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //return super.clone();
        Person tmp = (Person)super.clone();

        tmp.m = (Money)this.m.clone();
        return tmp;
    }
}
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person("张三");
        Person person2 = (Person)person.clone();
        System.out.println("修改之前："+person.m.money);
        System.out.println("修改之前："+person2.m.money);
        person2.m.money = 99.99;
        System.out.println("修改之后："+person.m.money);
        System.out.println("修改之后："+person2.m.money);


    }
}